
set type double
clear
set more 1

capture log using "log/descriptive statistics trade.log", replace
*1. 4.78% report multiple currencies within firm-country-product-month
*-> we keep the largest one in order to check switchers.
*2. 2.57% of firms switch (2.2% of value)


use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop valueI
	rename valueX value
	drop if missing(value)
     	rename country cty_code
       	drop if missing(currency)
    	gen euro="prod" if currency=="EUR"
    	replace euro="other" if currency~="EUR"

*1. check time persistent of euro
***********************************
*i within firm-destination-product
	
gcollapse (sum) value, by(vat cncode cty_code year month euro) 	
	
	egen id=group(vat cty_code cncode)
	egen t=group(year month)
	duplicates tag id t, gen(tag)
	tab tag

*keep largest value for duplicate currency		
	egen value_all=sum(value), by(id t)
	bys vat cncode cty_code month year (value): gen n=_n
	drop if tag>0 & n==1
	
	gen sh=value/value_all
	sum sh if sh~=1

	tsset id t
	gen euro_dum=1 if euro=="prod"
	replace euro_dum=0 if euro=="other"
	gen deuro=euro_dum-l.euro_dum
	drop if missing(deuro)
	tab deuro
	
	gen switch=deuro==1|deuro==-1
	
	gen value_switch=value*(switch==1)
	gcollapse (sum) value_switch value
	gen shvalue_switch=value_switch/value
	
	sum shvalue_switch
	

*2. share of euro within firm-destination that is 0/1
****************************************************
*i within firm-destination-product
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop valueI
	rename valueX value
	drop if missing(value)
     	rename country cty_code
       	drop if missing(currency)
       	
       	gen dum_curr="eur" if currency=="EUR"
       	replace dum_curr="other" if missing(dum_curr)
		
	gen value_euro=value*(dum_curr=="eur")
	gen value_other=value*(dum_curr=="other")
	
	gcollapse (sum) value*, by(vat cty_code)
	
	gen sh_euro=value_euro/value
	gen dum=sh_euro==1|sh_euro==0
	
	*share of obs that are zero/1 share of euro within firm-destination
	tab dum
	
	*share that have both euro and other between 20 and 80%
	gen mid=sh_euro>0.2 & sh_euro<0.8
	tab mid
	
	
*********************************
*share of fractional iotas in annual ERPT
use "../data/erpt_annual_estdata.dta", clear
	merge m:1 vat cty_code using ../data/currency_vehicle, keep(match master) nogen	
	drop if vehicle_other==1
	gen dum_euro= share_euro<0.2|share_euro>0.8
	tab dum_euro
	
	gen dum_us=share_us<0.2|share_us>0.8
	tab dum_us
	
	
*3. calculate Belgium's share of ex-Europe trade
*8 countries are in EU but not in eurozone
*Bulgaria, Czech republic, denmark, croatia, hungary, poland, romanica, sweden
*********************************************************************************
use ../../rawdata/X_2018, clear 
	rename land cty_code
	rename value xvalue
	
	gen eurozone = inlist(cty_code, "AT", "CY", "EE", "FI", "FR", "DE", "GR", "IE", "IT") 
	replace eurozone =1 if inlist(cty_code, "LV", "LT", "LU", "MT", "NL", "PT", "SK", "SI", "ES")
	replace eurozone = 1 if inlist(cty_code, "AD", "MC", "SM", "XK", "ME") 
	
	gen EU=inlist(cty_code, "AT", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR")
	replace EU=1 if inlist(cty_code, "DE", "GR", "HU", "IE", "IT", "LV", "LT", "LU")
	replace EU=1 if inlist(cty_code, "MT", "NL", "PL", "PT", "RO", "SK", "SI")
	replace EU=1 if inlist(cty_code, "ES", "SE")
*include UK	
	replace EU=1 if inlist(cty_code, "GB")
	
	egen europe=rowmax(EU eurozone)	

	gen xvalue_EU=xvalue*(EU==1)
	gen xvalue_EZ=xvalue*(eurozone==1)
	gen xvalue_EE=xvalue*(europe==1)
	
	collapse (sum) xvalue*, by(year)
	gen shexp_EU=xvalue_EU/xvalue
	gen shexp_EZ=xvalue_EZ/xvalue
	gen shexp_EE=xvalue_EE/xvalue

	gen shexp_xEE=1-xvalue_EE/xvalue
	sum sh*

save temp/xvalueEE, replace	



use ../../rawdata/M_2018, clear 
	rename land cty_code
	rename value mvalue
	
	gen eurozone = inlist(cty_code, "AT", "CY", "EE", "FI", "FR", "DE", "GR", "IE", "IT") 
	replace eurozone =1 if inlist(cty_code, "LV", "LT", "LU", "MT", "NL", "PT", "SK", "SI", "ES")
	replace eurozone = 1 if inlist(cty_code, "AD", "MC", "SM", "XK", "ME") 	
	
	gen EU=inlist(cty_code, "AT", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR")
	replace EU=1 if inlist(cty_code, "DE", "GR", "HU", "IE", "IT", "LV", "LT", "LU")
	replace EU=1 if inlist(cty_code, "MT", "NL", "PL", "PT", "RO", "SK", "SI")
	replace EU=1 if inlist(cty_code, "ES", "SE")
	replace EU=1 if inlist(cty_code, "GB")
	
	egen europe=rowmax(EU eurozone)	
	gen mvalue_EU=mvalue*(EU==1)
	gen mvalue_EZ=mvalue*(eurozone==1)
	gen mvalue_EE=mvalue*(europe==1)
	
	collapse (sum) mvalue*, by(year)
		
	gen shimp_EU=mvalue_EU/mvalue
	gen shimp_EZ=mvalue_EZ/mvalue
	gen shimp_EE=mvalue_EE/mvalue
	
	gen shimp_xEE=1-mvalue_EE/mvalue
	sum sh*
		
*merge on exports and calculate trade ratio
	merge 1:1 year using temp/xvalueEE, nogen
	gen trade=(mvalue+xvalue)/10^6
	gen gdp=459531.6
	gen trade_gdp=trade/gdp
	
	keep year sh* trade_gdp
	
export excel using ../results/sumstats/descriptive_stats_trade.xls, sheet(trade_shares) firstrow(variables) keepcellfmt replace
	


	
	
	
	
	